/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.token.dao.TokenDAO;
import com.opgea.token.domain.entities.Token;
import com.opgea.token.domain.entities.TokenDetails;
import com.opgea.token.domain.model.TokenStatusReplyModel;
import com.opgea.token.domain.qualifiers.TokenStatusQualifier;
import com.opgea.token.service.TokenService;
import com.opgea.token.web.dto.TokenDetailsDTO;

/**
 * 
 * @author Ramesh
 */
@Service
@Transactional
public class TokenServiceImpl implements TokenService {

    @Autowired
    private TokenDAO tokenDAO;

    @Override
    public Integer getMaxTokenNumber(Long companyId, String dateString) {
	if (tokenDAO.getMaxTokenNumber(companyId, dateString) == null) {
	    return 0;
	} else {
	    return tokenDAO.getMaxTokenNumber(companyId, dateString);
	}
    }

    @Override
    public TokenDetailsDTO create(TokenDetailsDTO tokenDetailsDTO) {
	Token token = new Token();
	tokenDAO.create(token);
	return tokenDetailsDTO;
    }

    @Override
    public TokenDetailsDTO update(TokenDetailsDTO tokenDetailsDTO) {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public TokenDetailsDTO find(Long id) {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public TokenDetailsDTO findAll() {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<TokenDetailsDTO> findAllByCompanyId(Long companyId, Date date) {
	List<Token> tokenList = new ArrayList<Token>();
	if (tokenDAO.findAll(companyId, date) != null) {
	    tokenList = tokenDAO.findAll(companyId, date);
	}
	List<TokenDetailsDTO> tokenDetailsList = new ArrayList<TokenDetailsDTO>();
	if (tokenList != null) {
	    for (Token token : tokenList) {
		TokenDetails tokenDetails = token.getTokenDetails();
		TokenDetailsDTO tdm = new TokenDetailsDTO();
		tdm.marshall(tokenDetails);
		tokenDetailsList.add(tdm);
	    }
	}
	return tokenDetailsList;
    }

    @Override
    public List<TokenDetailsDTO> findAllByCompanyIdAndStatus(Long companyId,
	    Date date, TokenStatusQualifier qualifier) {
	List<Token> tokenList = new ArrayList<Token>();
	if (tokenDAO.findAll(companyId, date) != null) {
	    tokenList = tokenDAO.findAllByStatus(companyId, date, qualifier);
	}
	List<TokenDetailsDTO> tokenDetailsList = new ArrayList<TokenDetailsDTO>();
	if (tokenList != null) {
	    for (Token token : tokenList) {
		TokenDetails tokenDetails = token.getTokenDetails();
		TokenDetailsDTO tdm = new TokenDetailsDTO();
		tdm.marshall(tokenDetails);
		tokenDetailsList.add(tdm);
	    }
	}
	return tokenDetailsList;
    }

    @Override
    public TokenStatusReplyModel tokenReply(Long companyId, Integer tokenNo) {
	List<Token> tokenList = tokenDAO.findTokensInQueue(companyId, tokenNo);
	TokenStatusReplyModel model = new TokenStatusReplyModel();
	if (tokenList.size() > 0) {
	    Token token = tokenList.get(tokenList.size() - 1);
	    TokenDetails details = token.getTokenDetails();
	    model.setTokenNo(token.getTokenNo());
	    model.setName(details.getName());
	    model.setCompanyId(companyId);
	    model.setCompanyName(token.getCompany().getName());
	    model.setDestinationName(details.getDestination().getName());
	    model.setTokenPositionInQueue(tokenList.size());
	    model.setStatus(token.getTokenDetails().getTokenStatus().name());
	    model.setFrom("OPGEA");
	}
	return model;
    }
}
